package com.common.thread;

import com.common.constant.LoggerConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 临时文件下超过2天的文件自动删除
 * 
 * @author 57300
 *
 */
public class TempFileDeleteThread implements Runnable {

	private final Logger logger = LoggerFactory.getLogger(TempFileDeleteThread.class);
	private String path;

	public TempFileDeleteThread(String path) {
		this.path = path;
	}

	@Override
	public void run() {
		deleteFiles(path);
	}

	/**
	 * 删除临时文件夹下超过2天的文件
	 */
	public void deleteFiles(String path) {
		try {
			File file = new File(path);// 这个文件夹下的
			if (file.exists()) {
				String[] files = file.list();
				for (String string : files) {
					try {
						String[] strs=string.split("\\.");
						String name = strs[0];
						Long time = Long.parseLong(name);
						if (System.currentTimeMillis() - time >= 2 * 24 * 60 * 60 * 1000.0) {
							File f = new File(path + "/" + string);
							if (f.exists()) {
								f.delete();
							}
						}
					} catch (Exception e) {
						logger.error(LoggerConstant.ERROR_MSG, e);
					}
				}
			}
		} catch (Exception e) {
			logger.error(LoggerConstant.ERROR_MSG, e);
		}
	}

}
